package cn.sanch.org.controller;

import cn.sanch.basic.annotation.PreAuthorize;
import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.PageList;
import cn.sanch.org.domain.Employee;
import cn.sanch.org.query.EmployeeQuery;
import cn.sanch.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：员工
 * @author sanch37
 * 2022/5/26
 *
 * 除了get\delete请求参数在地址栏中，用注解PathVariable获取参数赋值给后面的参数，
 * 其余有参数的用注解RequestBody获取请求的请求体中的参数
 *
 *
 */
@Api(value = "宠物之家的部门相关API", description = "员工的CRUD操作")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口：查询单个对象
     * @param id Long id
     * @return Employee
     *
     *
     * PathVariable Long id - 将请求地址中的参数赋值给id
     */
    @ApiOperation(value = "查询员工单个对象")
    @GetMapping("/{id}")

    public Employee queryOne(@PathVariable Long id) {
        return employeeService.findOne(id);
    }


    /**
     * 接口：查询所有对象
     * @return List<Employee>
     */
    @ApiOperation(value = "查询所有员工对象")
    @GetMapping
    public List<Employee> queryAll() {
        return employeeService.findAll();
    }


    /**
     * 接口：删除单个对象
     * @param id Long id
     * @return AjaxResult
     *
     * PathVariable Long id - 将请求地址中的参数赋值给id
     */
    @ApiOperation(value = "删除单个员工对象")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口：分页查询 + 高级查询
     * @param employeeQuery EmployeeQuery employeeQuery
     * @return PageList<Employee>
     *
     * RequestBody EmployeeQuery employeeQuery - 将请求体的参数，赋值给EmployeeQuery对象employeeQuery中
     */
    @ApiOperation(value = "员工分页查询 + 高级查询")
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }


    /**
     * 接口：增加或者修改
     * @param employee Employee employee
     * @return AjaxResult
     *
     * RequestBody Employee employee - 将请求体的参数，赋值给Employee对象employee中
     */
    @ApiOperation(value = "员工增加或者修改")
    @PutMapping

    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }

    }

    /**
     * 接口：批量删除员工
     * @param ids Long[] ids
     * @return AjaxResult
     *
     *
     */
    @ApiOperation(value = "批量删除员工")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }


    /**
     * 接口：根据店铺id查询改店铺的所有店员
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId) {
        return employeeService.list(shopId);
    }
}
